package com.windgo.controller.admin;

import com.windgo.bean.MarketCoupon;
import com.windgo.bean.MarketCouponUser;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.service.CouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:ZhouXin
 * @Date:2022/10/12 23:22
 * @Description:
 **/
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;


    @GetMapping("list")
    @RequiresPermissions("perms[admin:coupon:list]")
    public BaseRespVo list(BasePageInfo info,String name,Short type,Short status){
        CommonData<MarketCoupon> data=couponService.list(info,name,type,status);
        return BaseRespVo.ok(data);
    }

    /**
     *添加优惠券
     * @param marketCoupon
     * @return
     */
    @PostMapping("create")
    @RequiresPermissions("perms[admin:coupon:create]")
    public BaseRespVo create(@RequestBody MarketCoupon marketCoupon){
        if (marketCoupon.getMin().intValue() < 0 || marketCoupon.getDiscount().intValue() < 0 || marketCoupon.getLimit() < 1 || marketCoupon.getTotal() < 1 || marketCoupon.getDays() < 0 || marketCoupon.getMin().intValue() < marketCoupon.getDiscount().intValue()){
            return BaseRespVo.ok(404,"参数设置不合理！");
        }
        MarketCoupon data=couponService.create(marketCoupon);
        return BaseRespVo.ok(data);
    }


    /**
     * 修改优惠券信息
     * @param marketCoupon
     * @return
     */
    @PostMapping("update")
    @RequiresPermissions("perms[admin:coupon:update]")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon){
       MarketCoupon data= couponService.update(marketCoupon);
       return BaseRespVo.ok(data);
    }

    /**
     * 删除优惠券
     * @param marketCoupon
     * @return
     */
    @PostMapping("delete")
    @RequiresPermissions("perms[admin:coupon:delete]")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon){
        couponService.delete(marketCoupon);
        return BaseRespVo.ok();
    }

    /**
     * 优惠券详情的read
     * @param id
     * @return
     */
    @GetMapping("read")
    @RequiresPermissions("perms[admin:coupon:read]")
    public BaseRespVo read(Integer id){
        MarketCoupon data=couponService.read(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 优惠券详情中和显示该优惠券对应的用户信息
     * @param info
     * @param couponId
     * @return
     */
    @GetMapping("listuser")
    @RequiresPermissions("perms[admin:coupon:listuser]")
    public BaseRespVo listUser(BasePageInfo info,Integer couponId){
        CommonData<MarketCouponUser> data=couponService.listUser(info,couponId);
        return BaseRespVo.ok(data);
    }
}